package foundation.pcmUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jdom2.Element;
import org.jdom2.JDOMException;

import foundation.fileUtil.XMLFileUtil;

public class AllocationModelHelper {
	private XMLFileUtil xmlHelper;
	private String allocationFileName;
	
	public AllocationModelHelper(){
		
	}
	
	public AllocationModelHelper(String allocationFileName){
		this.allocationFileName = allocationFileName;
		File allocationFile = new File(allocationFileName);
		xmlHelper = new XMLFileUtil(allocationFile);	
	}	
	
	public List<Element> readNode(String pathName) throws JDOMException{
		return xmlHelper.readNode(pathName);
	}
	public List<Element> readNode(String pathName,String prefix,String url) throws JDOMException {
		return xmlHelper.readNode(pathName, prefix, url);
	}
	
	public synchronized void write() throws FileNotFoundException, IOException{
		xmlHelper.write(allocationFileName);
	}

	public void close(){
		xmlHelper.close(allocationFileName);
	}
	/**
	 * 在allocation模型中，根据entityName来获取serverID
	 * @param entityName
	 * @return
	 */
	public String getServerIDByEntityName(String entityName){
		String serverID = null;
		
		String path="/allocation:Allocation/"
				+ "allocationContexts_Allocation[@entityName='"+entityName+"']/"
				+"resourceContainer_AllocationContext";
		Element serverIDEle;
		try {
			serverIDEle = xmlHelper.readNode(path,"allocation",
					"http://palladiosimulator.org/PalladioComponentModel/Allocation/5.1" ).get(0);			
			String serverLink = serverIDEle.getAttributeValue("href");
			serverID=serverLink.replace("brs.resourceenvironment#", ""); 
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return serverID;
		
	}
	
	/**
	 * 根据entityName，更改AllocationContext的href属性
	 * @param entityName
	 * @param href
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public void  mdfyAllocation(String entityName,String href) throws FileNotFoundException, IOException {		
		String path = "/allocation:Allocation/"
				+ "allocationContexts_Allocation[@entityName='"
				+ entityName + "']/"
				+ "resourceContainer_AllocationContext";
		try {
			Element allocation = xmlHelper.readNode(path, "allocation",
					"http://palladiosimulator.org/PalladioComponentModel/Allocation/5.1").get(0);
			allocation.setAttribute("href",href);	
		}
		catch (JDOMException e) {
			e.printStackTrace();
		}	
		write();		
	}
	
	/**
	 * 分配模型中，通过ServerID获取组件实体名字
	 * @param serverID
	 * @return
	 */
	public List<String> getCompEntysBySvrID(String serverID,String prex){
		List<String> compEntys = new ArrayList<String>();
		String link = prex+".resourceenvironment#"+serverID;
		String path = "/allocation:Allocation/"
				+ "allocationContexts_Allocation";
		try {
			List<Element> compEntysElme = xmlHelper.readNode(path, "allocation",
					"http://palladiosimulator.org/PalladioComponentModel/Allocation/5.1");			
			for(Element compEnty:compEntysElme){
				if(compEnty.getChildren().get(0).getAttributeValue("href").toString().equals(link)){
					String compEntyName = compEnty.getAttributeValue("entityName");
					compEntys.add(compEntyName);
				}
			}
			
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return compEntys;
	}
	
	public String getResIDBySysID(String sysID){
		String resID = null;
		String sysHref = "brs.system#"+sysID;
		String path = "/allocation:Allocation/allocationContexts_Allocation/"
				+ "assemblyContext_AllocationContext[@href='"+ sysHref + "']";
		try {
			Element resIDElme = xmlHelper.readNode(path, "allocation",
					"http://palladiosimulator.org/PalladioComponentModel/Allocation/5.1").get(0);
			resID = resIDElme.getParentElement().getChildren().get(0)
					.getAttributeValue("href").replace("brs.resourceenvironment#", "");
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resID;
	}
	
	public XMLFileUtil getXmlHelper() {
		return xmlHelper;
	}

	public void setXmlHelper(XMLFileUtil xmlHelper) {
		this.xmlHelper = xmlHelper;
	}

	public String getAllocationFileName() {
		return allocationFileName;
	}

	public void setAllocationFileName(String allocationFileName) {
		this.allocationFileName = allocationFileName;
	}
	
}
